package com.pan.service.aspect;


import com.pan.entity.Result;
import com.pan.excep.enume.ErrorCode;
import com.pan.excep.error.ServiceException;
import com.pan.tools.RedisTools;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Aspect
@Component
public class DepositoryAspect {

    @Resource
    private RedisTools redisTools;

    @Pointcut(value = "execution(public * com.pan.service.impl.DepositoryServiceImpl.insert(String,..))")
    public void depositoryInsert(){}

    @Before("depositoryInsert()")
    public void checkLogin(JoinPoint joinPoint){
        Object[] objects = joinPoint.getArgs();
        String adminTicket = (String) objects[0];
        Result<Boolean> isAdmin = redisTools.exists("admin:"+adminTicket);
        if (isAdmin.getCode()==null||!isAdmin.getData()){
            throw  new ServiceException(ErrorCode.NOT_LOGIN);
        }


    }

}
